﻿using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table.RowProperties;
using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Core;
using DocumentFormat.OpenXml.Lite.Core.Interfaces;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table
{
    [XmlRoot("tr", Namespace = Constants.WordMain)]
    public class TableRow : XmlSerializable, IOfficeElement
    {
        [XmlIgnore]
        public Guid DocumentId { get; set; }

        [Optional]
        [XmlElement("trPr", Namespace = Constants.WordMain)]
        public TableRowProperties Properties { get; set; }

        [XmlElement("ins", Namespace = Constants.WordMain)]
        public InsertedRow InsertedRow { get; set; }

        [XmlElement("del", Namespace = Constants.WordMain)]
        public DeletedRow DeletedRow { get; set; }

        [XmlElement("trPrChange", Namespace = Constants.WordMain)]
        public RowChange RowChange { get; set; }

        [Optional]
        [XmlElement("tc", Namespace = Constants.WordMain)]
        public XmlList<TableCell> Cells { get; set; }

        [XmlIgnore]
        public TableCell this[int i]
        {
            get { return Cells[i]; }
            set { Cells[i] = value; }
        }

        public TableRow()
        {
            Cells = new XmlList<TableCell>();
            Cells.Changing += Rows_Changing;
        }

        public TableRow(int columns)
            : this()
        {
            for (int i = 0; i < columns; ++i)
            {
                Cells.Add(new TableCell());
            }
        }

        public TableCell AddRow()
        {
            TableCell c = new TableCell();
            c.DocumentId = this.DocumentId;
            Cells.Add(c);
            return c;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddProperty(ITableRowPropElement element)
        {
            var obj = Properties.Properties.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as ITableRowPropElement).Copy(element);
            }
            else
            {
                Properties.Properties.Add(element);
                return true;
            }

            return false;
        }

        #region Events

        void Rows_Changing(object sender, ListEventArgs<TableCell> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                obj.DocumentId = this.DocumentId;
            }
        }

        #endregion
    }

    [XmlRoot("trPr", Namespace = Constants.WordMain)]
    public class TableRowProperties
    {
        [Optional]
        [XmlElement("", Namespace = Constants.WordMain)]
        public XmlList<ITableRowPropElement> Properties { get; set; }

        public TableRowProperties()
        {
            Properties = new XmlList<ITableRowPropElement>();
            Properties.Changing += Properties_Changing;
        }

        void Properties_Changing(object sender, ListEventArgs<ITableRowPropElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(ITableRowPropElement)))
                {
                    var element = Properties.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + Properties.ToString());
                    }
                }
            }
        }
    }
}
